package com.wzy.gmall.pms.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.gmall.pms.entity.*;
import com.wzy.gmall.pms.mapper.*;
import com.wzy.gmall.pms.service.ProductService;
import com.wzy.gmall.vo.PageInfoVo;
import com.wzy.gmall.vo.product.PmsProductParam;
import com.wzy.gmall.vo.product.PmsProductQueryParam;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2020-02-15
 */
@org.springframework.stereotype.Service
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductAttributeValueMapper productAttributeValueMapper;
    @Autowired
    ProductFullReductionMapper productFullReductionMapper;
    @Autowired
    SkuStockMapper skuStockMapper;
    @Autowired
    ProductLadderMapper productLadderMapper;
    ThreadLocal<Long> threadLocal = new ThreadLocal<>();


    @Override

    public PageInfoVo productPageInfo(PmsProductQueryParam param) {

        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if (param.getBrandId() != null) {
            wrapper.eq("brand_id", param.getBrandId());
        }
        if (!StringUtils.isEmpty(param.getKeyword())) {
            wrapper.like("name", param.getKeyword());
        }
        if (param.getProductCategoryId() != null) {
            wrapper.eq("product_category_id", param.getProductCategoryId());
        }
        if (!StringUtils.isEmpty(param.getProductSn())) {
            wrapper.like("product_sn", param.getBrandId());
        }
        if (param.getPublishStatus() != null) {
            wrapper.eq("publish_status", param.getPublishStatus());
        }
        if (param.getVerifyStatus() != null) {
            wrapper.eq("verify_status", param.getVerifyStatus());
        }

        IPage<Product> page = productMapper.selectPage(new Page<Product>(param.getPageNum(), param.getPageSize()), wrapper);
        PageInfoVo pageInfoVo = new PageInfoVo(page.getTotal(), page.getPages(), param.getPageSize(),
                page.getRecords(), page.getCurrent());

        return pageInfoVo;
    }

    //大保存
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveProduct(PmsProductParam productParam) {
        ProductServiceImpl proxy = (ProductServiceImpl) AopContext.currentProxy();

        //1.pms_product:保存商品的基本信息
        proxy.saveBaseInfo(productParam);

        //pms_product_attribute_value:保存这个商品对相应的所有属性的值
        proxy.savaProductAttribuevalue(productParam);

        //一下都可一trycath互不影响
        //pms_product_full_reduction:保存商品的满减信息
        proxy.saveFullReduction(productParam);

        //pms_product_ladder;满减信息表;价格阶梯表
        proxy.saveProductLadder(productParam);

        //pms_sku_stock;sku库存表
        proxy.saveSkuStock(productParam);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveSkuStock(PmsProductParam productParam) {

        List<SkuStock> skuStockList = productParam.getSkuStockList();
        for (int i = 1; i <= skuStockList.size(); i++) {
            SkuStock skuStock = skuStockList.get(i);
            if (StringUtils.isEmpty(skuStock.getSkuCode())) {
                skuStock.setSkuCode(threadLocal.get() + "_" + i);
            }
            skuStock.setProductId(threadLocal.get());
            skuStockMapper.insert(skuStock);
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveProductLadder(PmsProductParam productParam) {

        List<ProductLadder> productLadderList = productParam.getProductLadderList();
        productLadderList.forEach((priductLadder) -> {
            priductLadder.setProductId(threadLocal.get());
            productLadderMapper.insert(priductLadder);
        });
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveFullReduction(PmsProductParam productParam) {
        List<ProductFullReduction> fullReductionList = productParam.getProductFullReductionList();
        fullReductionList.forEach((reduction) -> {
            reduction.setProductId(threadLocal.get());
            productFullReductionMapper.insert(reduction);
        });
    }

    /**
     * 保存商品基础信息;抽取方法
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveBaseInfo(PmsProductParam productParam) {
        //pms_product:保存商品的基本信息
        Product product = new Product();
        BeanUtils.copyProperties(productParam, product);
        productMapper.insert(product);
        threadLocal.set(product.getId());
    }


    //pms_product_attribute_value:保存这个商品对相应的所有属性的值
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void savaProductAttribuevalue(PmsProductParam productParam) {
        List<ProductAttributeValue> valueList = productParam.getProductAttributeValueList();
        valueList.forEach((item) -> {
            item.setProductId(threadLocal.get());
            productAttributeValueMapper.insert(item);
        });
    }
}
